package com.china08.yxyapi.api.manage.sch.platform.website.section;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSection.DisplayStyle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;

@CrossOrigin
@RestController("SectionUpdController")
public class SectionUpdController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        String pid = reqModel.pid;
        String outLink = reqModel.outLink;
        boolean isAudit = reqModel.isAudit;
        String sectionName = reqModel.sectionName;
        Integer sectionType = reqModel.sectionType;
        String displayStyle = reqModel.displayStyle;
        boolean showInHomePage = reqModel.showInHomePage;
        boolean showNavigation = reqModel.showNavigation;
        boolean showNavigationForMicroWebSite = reqModel.showNavigationForMicroWebSite;
        String schoolId = getProfile(request, response).getSchoolId();

        Assert.isTrue(StringUtils.isNotBlank(sectionName), "栏目名称不能为空");
        // 外链行
        if (sectionType == 1) {
            Assert.isTrue(StringUtils.isNotBlank(outLink), "外链地址不能为空");
            boolean b1 = outLink.startsWith("http://") || outLink.startsWith("https://");
            Assert.isTrue(b1, "请以http://或https://开头");
        } //
        else {
            outLink = null;
            Assert.isTrue(StringUtils.isNotBlank(displayStyle), "列表展示样式不能为空");
        }

        // 判断栏目是否存在
        SwsSection swsSection = swsSectionRepository.findById(id);
        Assert.isTrue(null != swsSection, "不存在当前栏目");

        swsSection.setPid(pid);
        swsSection.setSectionName(sectionName);
        swsSection.setSectionType(sectionType);
        swsSection.setIsAudit(isAudit);
        swsSection.setOutLink(outLink);
        if (displayStyle.equals("TEXT_LIST")) {
            swsSection.setDisplayStyle(DisplayStyle.TEXT_LIST);
        } else if (displayStyle.equals("IMG_LIST")) {
            swsSection.setDisplayStyle(DisplayStyle.IMG_LIST);
        } else {
            swsSection.setDisplayStyle(DisplayStyle.IMG_GRID);
        }
        swsSection.setShowInHomePage(showInHomePage);
        swsSection.setShowNavigation(showNavigation);
        swsSection.setShowNavigationForMicroWebSite(showNavigationForMicroWebSite);
        swsSection.setSchoolId(schoolId);
        swsSectionRepository.save(swsSection);
        // 保存
        return true;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 栏目名称
        public String sectionName;

        // 栏目类型
        public Integer sectionType;

        // 栏目id
        public String id;

        // 栏目关联id
        public String pid;

        // 是否首页显示
        public boolean showInHomePage;

        // 是否导航显示
        public boolean showNavigation;

        public boolean showNavigationForMicroWebSite;

        // 展示样式
        public String displayStyle;

        // 是否审核
        public boolean isAudit;

        // 外连接
        public String outLink;

    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
